const express = require('express')
var mongoose = require('mongoose');
const path = require('path')
const app = express();
var logAndReg = require('./router/logAndReg');
var upload = require('./router/upload');
var admin = require('./router/admin');
var bodyParser = require('body-parser')
var passport = require('passport')

app.use('/', express.static(__dirname + '/router/uploads/userImg'));
app.use(bodyParser.urlencoded({
    extended: true
})); //解析 x-www-form-urlencoded
app.use(bodyParser.json()); //无法演示 解析json数据依赖于urlencoded模块 必须同时应用
app.use(express.static(path.join(__dirname, '../client/dist/')))
app.use('/logAndReg', logAndReg);
app.use('/upload', upload);
app.use('/admin', admin);
/*当用户访问的url以/public开始，那么直接返回public目录下的文件*/

const dbUrl = process.env.NODE_ENV === 'production' ? 'mongodb://xza:www.xza.com@127.0.0.1:19889/ShopCar?authSource=admin' : 'mongodb://localhost:4408/ShopCar'
mongoose.connect(dbUrl, {
    useNewUrlParser: true
}, function (err) {
    if (err) {
        throw err
    } else {
        console.log('Is OK')
        app.listen(8001, function () {
            console.log('Runing server 8001')
        })
    }
})

app.use(passport.initialize());
require('./config/passport')(passport);